package ru.amse.baltijsky.javascheme.nodeshape;

import ru.amse.baltijsky.javascheme.util.Size;
import ru.amse.baltijsky.javascheme.util.VisConst;

import java.awt.*;

/**
 * Represents a diamond (rhombus) shape for the If node housing the code frame.
 */
public class IfNodeShape extends AbstractNodeShape {
    /**
     * The angle between code frame and shape edges.
     */
    private static final double ALPHA = Math.PI / 8;
    /**
     * The offset for the code frame inside the box inscribed into the diamond.
     */
    private static final Size OFFSET = new Size(2, 2);

    /**
     * Creates a new shape for If node that contains the specified code.
     *
     * @param code the node code
     */
    public IfNodeShape(String code) {
        super(code);
    }

    public void draw(int x, int y, Graphics g) {
        Size size = getBoundRectSize(g);
        int halfX = size.x / 2;
        int halfY = size.y / 2;
        Color oldColor = g.getColor();

        // draw a diamond-shaped node
        g.setColor(VisConst.SCHEME_COL);
        g.drawLine(x - halfX, y, x, y - halfY);
        g.drawLine(x, y - halfY, x + halfX, y);
        g.drawLine(x + halfX, y, x, y + halfY);
        g.drawLine(x, y + halfY, x - halfX, y);

        // draw label
        Size codeSize = getCodeSize(g);
        g.setColor(VisConst.TEXT_COL);
        g.drawString(getCode(), x - codeSize.x / 2, y + OFFSET.y);

        g.setColor(oldColor);
    }

    public Size getBoundRectSize(Graphics g) {
        Size codeSize = getCodeSize(g);
        // basicSize is for the box inscribed into the rhombus that contains the code frame
        int basicSizeX = OFFSET.x + codeSize.x + OFFSET.x;
        int basicSizeY = OFFSET.y + codeSize.y + OFFSET.y;
        int sizeX = (int) (basicSizeX + basicSizeY / Math.tan(ALPHA));
        int sizeY = (int) (basicSizeX * Math.tan(ALPHA) + basicSizeY);
        return new Size(sizeX, sizeY);
    }
}
